package sample.stormax.pres.news.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

import sample.stormax.pres.news.R;

/**
 * Created by StormAX on 2018/7/2 15:39.
 * Mail yuanmenglong97@qq.com
 */

public class TypeAdapter extends BaseAdapter {

    private static final String TAG = "TypeAdapter";

    private Context context;

    private int[] imgs;
    private String[] types;

    private ArrayList<String> selectedTypes = new ArrayList<>();
    private int length;

    public TypeAdapter(Context context, int[] imgs, String[] types) {
        this.context = context;
        this.imgs = imgs;
        this.types = types;
        selectedTypes.add("top");
    }

    public ArrayList<String> getSelectedTypes() {
        return selectedTypes;
    }

    public int getLength() {
        return length;
    }

    @Override
    public int getCount() {
        return types.length;
    }

    @Override
    public Object getItem(int position) {
        return types[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ImageView imageView;
        TextView textView;
        CheckBox checkBox;
        ViewHolder holder;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            v = inflater.inflate(R.layout.item_grid_icon,null);
            imageView = v.findViewById(R.id.img_icon);
            textView = v.findViewById(R.id.txt_icon);
            checkBox = v.findViewById(R.id.check_icon);
            holder = new ViewHolder(imageView,textView,checkBox,v);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
            imageView = holder.iconImage;
            textView = holder.iconText;
            checkBox = holder.iconCheck;
        }

        imageView.setImageResource(imgs[position]);
        textView.setText(types[position]);
        return v;
    }

    class ViewHolder {
        final ImageView iconImage;
        final TextView iconText;
        final CheckBox iconCheck;
        final View mView;

        public ViewHolder(ImageView iconImage, final TextView iconText, final CheckBox iconCheck, View mView) {
            this.iconImage = iconImage;
            this.iconText = iconText;
            this.iconCheck = iconCheck;
            this.mView = mView;

            mView.setOnClickListener(new View.OnClickListener() {
                boolean state = false;
                @Override
                public void onClick(View v) {
                    state = !state;
                    iconCheck.setChecked(state);
                    if (state) {
                        selectedTypes.add((String) iconText.getText());
                        Log.d(TAG, Arrays.toString(selectedTypes.toArray()));
                        Log.d(TAG, selectedTypes.size() + "");
                    } else {
                        selectedTypes.remove((String) iconText.getText());
                        Log.d(TAG, Arrays.toString(selectedTypes.toArray()));
                        Log.d(TAG, selectedTypes.size() + "");
                    }
                }
            });
        }

    }
}
